package com.pengtu.web.project;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.UserQualification;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.app.SystemManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Result(name = BaseActionSupport.RELOAD, location = "/project/sign/org/userqualification.action?deptId=${deptId}&projectId=${projectId}&typeId=${typeId}&organizationId=${organizationId}&userdeptId=${userdeptId}&userId=${userId}", type = "redirect")
public class UserqualificationAction extends
		BaseActionSupport<UserQualification> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;
	private AccountManager accountMananger;
	private SystemManager systemManager;

	private String id;
	private UserQualification userQualification;
	private String userId;
	private List<UserQualification> userQuaList;
	private Page<UserQualification> page = new Page<UserQualification>(10);
	private String projectId;
	private String deptId;
	private String userdeptId;
	private String typeId;
	private String organizationId;
	private Attachment attachments;

	@Override
	public UserQualification getModel() {
		// TODO Auto-generated method stub
		return userQualification;
	}

	@Action("/project/sign/org/userqualification")
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(userId))
			userQuaList = accountMananger.findListUserQua("user.id", userId);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		

		if (null != attachments) {
			attachments.setTableId(userQualification.getId());
			attachments.setTableField("UserQua_qualificationfile");
			attachments.setTableName("UserQualification");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(), attachments);
			userQualification.setQualificationfile(attachments.getUpfileFileName());
			userQualification.setAttachment(attachments);
		}
		if (StringUtils.isNotEmpty(userId))
			userQualification.setUser(accountMananger.getUser(userId));
		accountMananger.saveUserQualification(userQualification);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"用户资质:"+userQualification.getId());
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountMananger.deleteUserQualification(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			userQualification = accountMananger.findByUserQua("id", id);
		} else {
			userQualification = new UserQualification();
		}
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Page<UserQualification> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setAccountMananger(AccountManager accountMananger) {
		this.accountMananger = accountMananger;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getUserdeptId() {
		return userdeptId;
	}

	public void setUserdeptId(String userdeptId) {
		this.userdeptId = userdeptId;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public UserQualification getUserQualification() {
		return userQualification;
	}

	public List<UserQualification> getUserQuaList() {
		return userQuaList;
	}

	public Attachment getAttachments() {
		return attachments;
	}

	public void setAttachments(Attachment attachments) {
		this.attachments = attachments;
	}

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

}
